<template>
	<el-dialog :title="`${type}`" v-model="visible" :close-on-click-modal="false" draggable width="500">
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="22" class="mb20">
					<el-form-item label="备注说明" prop="remarks">
						<el-input v-model="form.remarks" placeholder="请输入备注说明" maxlength="100" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { useMessage } from '/@/hooks/message';
import { enableOrDisable } from '/@/api/platform/basicDevice';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
const type = ref('');
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	state: 10,
	remarks: '',
});

// 定义校验规则
const dataRules = ref({
	remarks: [{ required: true, message: '说明不能为空', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (id: string, state: number, tp: string) => {
	visible.value = true;
	form.id = id;
	form.state = state;
	type.value = tp;

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		await enableOrDisable({ id: form.id, useState: form.state === 10 ? 20 : 10, remarks: form.remarks });
		useMessage().success(`${type.value}成功`);
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
